package edu.zut.dao.impl;

import edu.zut.dao.EmpDao;
import edu.zut.pojo.Employee;
import edu.zut.pojo.Page;
import edu.zut.util.DBUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//员工模块数据层实现类
public class EmpDaoImpl implements EmpDao {
    @Override
    public List<Employee> queryEmp() {

        String sql = "select e.* , d.dep_name from employee e, department d where e.dep_id = d.id";
        ResultSet rs = DBUtil.query(sql, null);

        List<Employee> emps = new ArrayList<>();
        try {
            while (rs.next()){
                Employee employee = new Employee();
                employee.setId(rs.getInt("id"));
                employee.setAddress(rs.getString("address"));
                employee.setAge(rs.getInt("age"));
                employee.setEdu(rs.getString("edu"));
                employee.setDuty(rs.getString("duty"));
                employee.setDep_id(rs.getInt("dep_id"));
                employee.setMajor(rs.getString("major"));
                employee.setName(rs.getString("name"));
                employee.setPhone(rs.getString("phone"));
                employee.setEntry_time(rs.getDate("entry_time"));
                employee.setSchool(rs.getString("school"));
                employee.setSex(rs.getString("sex"));
                employee.setDep_name(rs.getString("dep_name"));
                emps.add(employee);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return emps;
    }


    //查询信息根据员工姓名
    @Override
    public List<Employee> findEmpByName(String emp_name) {
        String sql = "select e.* , d.dep_name from employee e, department d where e.dep_id = d.id and name like ?";
        ResultSet rs = DBUtil.query(sql, "%" + emp_name + "%");

        List<Employee> emps = new ArrayList<>();
        try {
            while (rs.next()){
                Employee employee = new Employee();
                employee.setId(rs.getInt("id"));
                employee.setAddress(rs.getString("address"));
                employee.setAge(rs.getInt("age"));
                employee.setEdu(rs.getString("edu"));
                employee.setDuty(rs.getString("duty"));
                employee.setDep_id(rs.getInt("dep_id"));
                employee.setMajor(rs.getString("major"));
                employee.setName(rs.getString("name"));
                employee.setPhone(rs.getString("phone"));
                employee.setEntry_time(rs.getDate("entry_time"));
                employee.setSchool(rs.getString("school"));
                employee.setSex(rs.getString("sex"));
                employee.setDep_name(rs.getString("dep_name"));
                emps.add(employee);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return emps;
    }

    @Override
    public int deleteEmp(int id) {
        String sql = "delete from employee where id = ?";
        int i = DBUtil.update(sql, id);
        return i;
    }
}
